/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "geofence_module_test.h"

#include <iostream>
#include <chrono>
#include <thread>
#include <gtest/gtest.h>
#include <if_system_ability_manager.h>
#include <ipc_skeleton.h>
#include <string_ex.h>
#include <unistd.h>

#include "geofence_common.h"
#include "geofence_client.h"
#include "geofence_policy.h"

using namespace testing::ext;
using namespace OHOS::Msdp;
using namespace OHOS;
using namespace std;

void GeofenceModuleTest::GeofenceTestCallback::OnGeofenceChanged(const GeofenceDataUtils::GeofenceData& geofenceData)
{
    GTEST_LOG_(INFO) << "GeofenceTestCallback type: " << geofenceData.type;
    GTEST_LOG_(INFO) << "GeofenceTestCallback value: " << geofenceData.callbackvalue;
    EXPECT_EQ(true, geofenceData.type == (GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POINT_GEOFENCE) ||
        (geofenceData.type == GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POI_GEOFENCE))<< "GeofenceTestCallback falied";
}

/**
 * @tc.name: GeofenceTestSubscribe
 * @tc.desc: test SubscribePoint and Algorithm and Callback in client
 * @tc.type: FUNC
 */
HWTEST_F (GeofenceModuleTest, GeofenceTestCallback001, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POINT_GEOFENCE;
    auto& geofenceClient = GeofenceClient::GetInstance();
    sptr<IgeofenceCallback> cb = new GeofenceTestCallback();
    GeofenceDataUtils::PointOptions pointOptions;
    pointOptions.centerCoordinate = {121.594485, 38.888057};
    pointOptions.radius = 500.0;
    GTEST_LOG_(INFO) << "Start register";
    geofenceClient.SubscribePoint(type, pointOptions, cb);
    GTEST_LOG_(INFO) << "Cancell register";
    geofenceClient.UnSubscribePoint(type, pointOptions, cb);
}

/**
 * @tc.name: GeofenceTestSubscribe
 * @tc.desc: test SubscribePoint and Algorithm and Callback in client
 * @tc.type: FUNC
 */
HWTEST_F (GeofenceModuleTest, GeofenceTestCallback002, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POINT_GEOFENCE;
    auto& geofenceClient = GeofenceClient::GetInstance();
    sptr<IgeofenceCallback> cb = new GeofenceTestCallback();
    GeofenceDataUtils::PointOptions pointOptions;
    pointOptions.centerCoordinate = {121.594485, 38.888057};
    pointOptions.radius = 500.0;
    GTEST_LOG_(INFO) << "Start register";
    geofenceClient.SubscribePoint(type, pointOptions, cb);
    GTEST_LOG_(INFO) << "Cancell register";
    geofenceClient.UnSubscribePoint(type, pointOptions, cb);
}

/**
 * @tc.name: GeofenceTestSubscribe
 * @tc.desc: test SubscribePoint and Algorithm and Callback in client
 * @tc.type: FUNC
 */
HWTEST_F (GeofenceModuleTest, GeofenceTestCallback003, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POINT_GEOFENCE;
    auto& geofenceClient = GeofenceClient::GetInstance();
    sptr<IgeofenceCallback> cb = new GeofenceTestCallback();
    GeofenceDataUtils::PointOptions pointOptions;
    pointOptions.centerCoordinate = {121.594485, 38.888057};
    pointOptions.radius = 500.0;
    GTEST_LOG_(INFO) << "Start register";
    geofenceClient.SubscribePoint(type, pointOptions, cb);
    GTEST_LOG_(INFO) << "Cancell register";
    geofenceClient.UnSubscribePoint(type, pointOptions, cb);
}

/**
 * @tc.name: GeofenceTestSubscribe
 * @tc.desc: test SubscribePoi and Algorithm and Callback in client
 * @tc.type: FUNC
 */
HWTEST_F (GeofenceModuleTest, GeofenceTestCallback004, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POI_GEOFENCE;
    auto& geofenceClient = GeofenceClient::GetInstance();
    sptr<IgeofenceCallback> cb = new GeofenceTestCallback();
    GeofenceDataUtils::PoiOptions poiOptions;
    poiOptions.keyword = "XingHaiGuangChang";
    poiOptions.poiType = "GuangChang";
    poiOptions.poiCity = "DaLian";
    GTEST_LOG_(INFO) << "Start register";
    geofenceClient.SubscribePoi(type, poiOptions, cb);
    GTEST_LOG_(INFO) << "Cancell register";
    geofenceClient.UnSubscribePoi(type, poiOptions, cb);
}

/**
 * @tc.name: GeofenceTestSubscribe
 * @tc.desc: test SubscribePoi and Algorithm and Callback in client
 * @tc.type: FUNC
 */
HWTEST_F (GeofenceModuleTest, GeofenceTestCallback005, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POI_GEOFENCE;
    auto& geofenceClient = GeofenceClient::GetInstance();
    sptr<IgeofenceCallback> cb = new GeofenceTestCallback();
    GeofenceDataUtils::PoiOptions poiOptions;
    poiOptions.keyword = "XingHaiGuangChang";
    poiOptions.poiType = "GuangChang";
    poiOptions.poiCity = "DaLian";
    GTEST_LOG_(INFO) << "Start register";
    geofenceClient.SubscribePoi(type, poiOptions, cb);
    GTEST_LOG_(INFO) << "Cancell register";
    geofenceClient.UnSubscribePoi(type, poiOptions, cb);
}

/**
 * @tc.name: GeofenceTestSubscribe
 * @tc.desc: test SubscribePoi and Algorithm and Callback in client
 * @tc.type: FUNC
 */
HWTEST_F (GeofenceModuleTest, GeofenceTestCallback006, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_ADD_POI_GEOFENCE;
    auto& geofenceClient = GeofenceClient::GetInstance();
    sptr<IgeofenceCallback> cb = new GeofenceTestCallback();
    GeofenceDataUtils::PoiOptions poiOptions;
    poiOptions.keyword = "XingHaiGuangChang";
    poiOptions.poiType = "GuangChang";
    poiOptions.poiCity = "DaLian";
    GTEST_LOG_(INFO) << "Start register";
    geofenceClient.SubscribePoi(type, poiOptions, cb);
    GTEST_LOG_(INFO) << "Cancell register";
    geofenceClient.UnSubscribePoi(type, poiOptions, cb);
}

/**
 * @tc.name: GeofenceTestSetFoolproofTactics
 * @tc.desc: test set foolproof tactics
 * @tc.type: FUNC
 */

HWTEST_F (GeofenceModuleTest, GeofenceTestset007, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_NO_MORE_REMINDERS;
    auto& geofenceClient = GeofenceClient::GetInstance();
    GTEST_LOG_(INFO) << "Start set";
    geofenceClient.SetFoolproofTactics(type, 3);
    GTEST_LOG_(INFO) << "End set";
}

/**
 * @tc.name: GeofenceTestSetFoolproofTactics
 * @tc.desc: test set foolproof tactics
 * @tc.type: FUNC
 */

HWTEST_F (GeofenceModuleTest, GeofenceTestset008, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_NO_MORE_REMINDERS;
    auto& geofenceClient = GeofenceClient::GetInstance();
    GTEST_LOG_(INFO) << "Start set";
    geofenceClient.SetFoolproofTactics(type, 5);
    GTEST_LOG_(INFO) << "End set";
}

/**
 * @tc.name: GeofenceTestSetFoolproofTactics
 * @tc.desc: test set foolproof tactics
 * @tc.type: FUNC
 */

HWTEST_F (GeofenceModuleTest, GeofenceTestset009, TestSize.Level0)
{
    GeofenceDataUtils::GeofenceType type = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_NO_MORE_REMINDERS;
    auto& geofenceClient = GeofenceClient::GetInstance();
    GTEST_LOG_(INFO) << "Start set";
    geofenceClient.SetFoolproofTactics(type, 10);
    GTEST_LOG_(INFO) << "End set";
}

